﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Microsoft.Surface.Presentation.Controls;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Controls;

namespace WPFMitsuControlsv2
{
    public class SurfaceHeaderedContentControl : SurfaceContentControl
    {

        internal static readonly DependencyPropertyKey HasHeaderPropertyKey = DependencyProperty.RegisterReadOnly(
            "HasHeader", typeof(bool), typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(false));
        public static readonly DependencyProperty HasHeaderProperty = HasHeaderPropertyKey.DependencyProperty;

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
            "Header", typeof(object), typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(SurfaceHeaderedContentControl.OnHeaderChanged)));

        public static readonly DependencyProperty HeaderStringFormatProperty = DependencyProperty.Register(
            "HeaderStringFormat", typeof(string), typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(SurfaceHeaderedContentControl.OnHeaderStringFormatChanged)));

        public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register(
            "HeaderTemplate", typeof(DataTemplate), typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(SurfaceHeaderedContentControl.OnHeaderTemplateChanged)));

        public static readonly DependencyProperty HeaderTemplateSelectorProperty = DependencyProperty.Register(
            "HeaderTemplateSelector", typeof(DataTemplateSelector), typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(SurfaceHeaderedContentControl.OnHeaderTemplateSelectorChanged)));

        // Methods
        static SurfaceHeaderedContentControl()
        {
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(SurfaceHeaderedContentControl), new FrameworkPropertyMetadata(typeof(SurfaceHeaderedContentControl)));
        }

        protected virtual void OnHeaderChanged(object oldHeader, object newHeader)
        {
            base.RemoveLogicalChild(oldHeader);
            base.AddLogicalChild(newHeader);
        }

        private static void OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            SurfaceHeaderedContentControl control = (SurfaceHeaderedContentControl)d;
            control.SetValue(HasHeaderPropertyKey, (e.NewValue != null));
            control.OnHeaderChanged(e.OldValue, e.NewValue);
        }

        protected virtual void OnHeaderStringFormatChanged(string oldHeaderStringFormat, string newHeaderStringFormat)
        {
        }

        private static void OnHeaderStringFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SurfaceHeaderedContentControl)d).OnHeaderStringFormatChanged((string)e.OldValue, (string)e.NewValue);
        }

        protected virtual void OnHeaderTemplateChanged(DataTemplate oldHeaderTemplate, DataTemplate newHeaderTemplate)
        {
        }

        private static void OnHeaderTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SurfaceHeaderedContentControl)d).OnHeaderTemplateChanged((DataTemplate)e.OldValue, (DataTemplate)e.NewValue);
        }

        protected virtual void OnHeaderTemplateSelectorChanged(DataTemplateSelector oldHeaderTemplateSelector, DataTemplateSelector newHeaderTemplateSelector)
        {
        }

        private static void OnHeaderTemplateSelectorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((SurfaceHeaderedContentControl)d).OnHeaderTemplateSelectorChanged((DataTemplateSelector)e.OldValue, (DataTemplateSelector)e.NewValue);
        }


        [Bindable(false), Browsable(false)]
        public bool HasHeader
        {
            get
            {
                return (bool)base.GetValue(HasHeaderProperty);
            }
        }

        [Localizability(LocalizationCategory.Label), Category("Content"), Bindable(true)]
        public object Header
        {
            get
            {
                return base.GetValue(HeaderProperty);
            }
            set
            {
                base.SetValue(HeaderProperty, value);
            }
        }

        [Bindable(true)]
        public string HeaderStringFormat
        {
            get
            {
                return (string)base.GetValue(HeaderStringFormatProperty);
            }
            set
            {
                base.SetValue(HeaderStringFormatProperty, value);
            }
        }

        [Category("Content"), Bindable(true)]
        public DataTemplate HeaderTemplate
        {
            get
            {
                return (DataTemplate)base.GetValue(HeaderTemplateProperty);
            }
            set
            {
                base.SetValue(HeaderTemplateProperty, value);
            }
        }

        [Category("Content"), Bindable(true)]
        public DataTemplateSelector HeaderTemplateSelector
        {
            get
            {
                return (DataTemplateSelector)base.GetValue(HeaderTemplateSelectorProperty);
            }
            set
            {
                base.SetValue(HeaderTemplateSelectorProperty, value);
            }
        }
    }
}


